﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.Dal.IncoherencesFix
{
    using System.Collections.Generic;
    using System.Linq;

    using AutoMapper;

    using NHibernate.Linq;

    using Probel.Ldoc.Dal.Configuration;
    using Probel.Ldoc.Dal.Entities;
    using Probel.Ldoc.Dal.Helpers;
    using Probel.Ldoc.Services.Dto;

    internal class ProfessionSpellingFix
    {
        #region Methods

        public IEnumerable<ProfessionCardDto> GetDoubloons(string name)
        {
            name = name.RemoveDiacriticsLowerCase();
            using (var session = DalManager.SessionFactory.OpenSession())
            {
                var doubloons = (from item in session.Query<Profession>().ToList()
                                 where item.Name.RemoveDiacriticsLowerCase() == name
                                 select item);
                return Mapper.Map<IEnumerable<Profession>, IEnumerable<ProfessionCardDto>>(doubloons);
            }
        }

        public IEnumerable<ProfessionCardDto> GetIncoherences()
        {
            using (var session = DalManager.SessionFactory.OpenSession())
            {
                var incoherences = (from item in session.Query<Profession>().ToList()
                                    group item by item.Name.RemoveDiacriticsLowerCase() into grp
                                    where grp.ToList().Count() > 1
                                    select grp.ToList().First());
                return Mapper.Map<IEnumerable<Profession>, IEnumerable<ProfessionCardDto>>(incoherences);
            }
        }

        internal void Fix(ProfessionCardDto profession)
        {
            using (var session = DalManager.SessionFactory.OpenSession())
            {
                var fix = session.Get<Profession>(profession.Id);
                var name = fix.Name.RemoveDiacriticsLowerCase();

                var incoherences = (from item in session.Query<Profession>().ToList()
                                    where item.Name.RemoveDiacriticsLowerCase() == name
                                       && item.Id != profession.Id
                                    select item);

                var query = session.CreateSQLQuery(string.Format("UPDATE patient SET Profession_Id = {0} WHERE Profession_Id in ({1})"
                                                               , profession.Id
                                                               , string.Join(",", incoherences.Select(e => e.Id))));
                query.ExecuteUpdate();

                foreach (var i in incoherences) { session.Delete(i); }

                session.Flush();
            }
        }

        #endregion Methods
    }
}